﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DataAccess;
using System.Data;

namespace XuaNhapKho
{
    public partial class NhapKho : System.Web.UI.Page
    {
        private NhapKhoDataTable dtNhapKho = new NhapKhoDataTable();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Session["User"] == null)
                {
                    Response.Redirect("/Default.aspx");
                }
                else
                {
                    DataTable dt = (DataTable)Session["User"];

                    foreach (DataRow rows in dt.Rows)
                    {
                        ltlTenNhanVien.Text = rows["HoVaTen"].ToString();
                        ltlSoDienThoai.Text = rows["SoDienThoai"].ToString();
                        ltlDiaChi.Text = rows["DiaChi"].ToString();

                    }
                }

                txtNgayNhap.Text = String.Format("{0:dd/MM/yyyy}", DateTime.Now);
                txtNgayNhap.Enabled = false;
                ddlDonVi.DataSource = DonViDAO.GetAll();
                ddlDonVi.DataValueField = "MaDonVi";
                ddlDonVi.DataTextField = "TenDonVi";
                ddlDonVi.DataBind();

                ddlNhaCungCap.DataSource = NhaCungCapDAO.GetAll();
                ddlNhaCungCap.DataValueField = "MaNhaCungCap";
                ddlNhaCungCap.DataTextField = "TenNhaCungCap";
                ddlNhaCungCap.DataBind();

                ddlNhomSanPham.DataSource = NhomSanPhamDAO.GetAll();
                ddlNhomSanPham.DataValueField = "MaNhomSanPham";
                ddlNhomSanPham.DataTextField = "TenNhomSanPham";
                ddlNhomSanPham.DataBind();

                if (Session["NhapKhoDataTable"] == null)
                {
                    Session["NhapKhoDataTable"] = dtNhapKho;
                }
                else
                {
                    Session["NhapKhoDataTable"] = dtNhapKho;
                  
                    loadGridViewNhapKho();
                }



            }
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {

            dtNhapKho = (NhapKhoDataTable)Session["NhapKhoDataTable"];
            string masanpham = txtMaSanPham.Text;
            string tensanpham = txtTenSanPham.Text;
            string vat = txtVAT.Text;
            string madonvi = ddlDonVi.SelectedValue;
            string tendonvi = ddlDonVi.SelectedItem.Text;
            string nhacungcap = ddlNhaCungCap.SelectedItem.Text;
            string manhacungcap = ddlNhaCungCap.SelectedValue;
            string soluong = txtSoLuong.Text;
            string dongia = txtGiaSanPham.Text;
            string ghichu = txtGhiChu.Text;
            string tennhomsanpham = ddlNhomSanPham.SelectedItem.Text;
            string manhomsanpham = ddlNhomSanPham.SelectedValue;
            int manhanvien=0;
            DataTable dt = (DataTable)Session["User"];

            foreach (DataRow rows in dt.Rows)
            {
                manhanvien = int.Parse(rows["MaNhanVien"].ToString());
            }
            
            string ngaynhap = txtNgayNhap.Text;
            dtNhapKho.InsertUpdate(masanpham, tensanpham, int.Parse(soluong), double.Parse(dongia), double.Parse(vat),
                ngaynhap, manhanvien, int.Parse(madonvi), tendonvi, ghichu, int.Parse(manhomsanpham), tennhomsanpham, int.Parse(manhacungcap), nhacungcap);
            Session["NhapKhoDataTable"] = dtNhapKho;

            loadGridViewNhapKho();
            txtMaSanPham.Enabled = true;

        }
        public void loadGridViewNhapKho()
        {
            dtNhapKho = (NhapKhoDataTable)Session["NhapKhoDataTable"];
            grvNhapKho.DataSource = dtNhapKho.GetTable();
            grvNhapKho.DataBind();

            ltlCount.Text = String.Format("{0:0,0}", dtNhapKho.GetTotalCount());
            ltlTotalMoney.Text = String.Format("{0:0,0}", dtNhapKho.GetTotalMoney());
            ltlTotalVat.Text = String.Format("{0:0,0}", dtNhapKho.GetTotalMoneyVAT());

            txtMaSanPham.Text = "";

        }
        protected void btnPrint_Click(object sender, EventArgs e)
        {

        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            dtNhapKho = (NhapKhoDataTable)Session["NhapKhoDataTable"];

            DataTable dt = dtNhapKho.GetTable();

            double tongtien = dtNhapKho.GetTotalMoney();
            int hoadon = 1;
            foreach (DataRow row in dt.Rows)
            {

                string masanpham = row["MaSanPham"].ToString();
                string tensanpham = row["TenSanPham"].ToString();
                string ngaynhap = row["NgayNhap"].ToString();
                string soluong = row["SoLuong"].ToString();
                string ghichu = row["GhiChu"].ToString();
                string giasanpham = row["GiaSanPham"].ToString();
                string vat = row["VAT"].ToString();
                string madonvi = row["MaDonVi"].ToString();
                string manhacungcap = row["MaNhaCungCap"].ToString();
                string manhomsanpham = row["MaNhomSanPham"].ToString();

                string manhanvien = row["MaNhanVien"].ToString();
                NhapKhoDAO.Insert(tongtien,masanpham,tensanpham, int.Parse(soluong), double.Parse(giasanpham), double.Parse(vat),int.Parse(manhacungcap), ghichu, int.Parse(madonvi), ngaynhap,
                    int.Parse(manhanvien), int.Parse(manhomsanpham), hoadon);
                hoadon = 0;
            }
            btnSave.Enabled = false;
            Session["NhapKhoDataTable"] = null;
            Response.Redirect("/NhapKho.aspx");
        }

        protected void lbtnUpdate_Command(object sender, CommandEventArgs e)
        {
            dtNhapKho = (NhapKhoDataTable)Session["NhapKhoDataTable"];

            DataRow row = dtNhapKho.View(e.CommandArgument.ToString());
            txtMaSanPham.Text = row["MaSanPham"].ToString();
            txtMaSanPham.Enabled = false;
            txtNgayNhap.Text = row["NgayNhap"].ToString();
            txtSoLuong.Text = row["SoLuong"].ToString();
            txtTenSanPham.Text = row["TenSanPham"].ToString();
            txtGhiChu.Text = row["GhiChu"].ToString();
            txtGiaSanPham.Text = row["GiaSanPham"].ToString();
            txtVAT.Text = row["VAT"].ToString();
            ddlDonVi.SelectedValue = row["MaDonVi"].ToString();
            ddlNhaCungCap.SelectedValue = row["MaNhaCungCap"].ToString();
            ddlNhomSanPham.SelectedValue = row["MaNhomSanPham"].ToString();
        }

        protected void lbtnRemove_Command(object sender, CommandEventArgs e)
        {
            dtNhapKho = (NhapKhoDataTable)Session["NhapKhoDataTable"];
            dtNhapKho.Delete(e.CommandArgument.ToString());
            txtMaSanPham.Text = "";
            loadGridViewNhapKho();
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            Session["NhapKhoDataTable"] = dtNhapKho;
            loadGridViewNhapKho();
        }
    }
}